#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2019 crane <crane@gosun>
#
# Distributed under terms of the MIT license.

"""

"""

class Solution:
    def __init__(self, decimal_fraction, to_radix=2):
        assert decimal_fraction < 1.0

        self.to_radix = to_radix
        self.decimal_fraction = decimal_fraction

    def binary_fraction(self, precision=15):
        # 默认精确到10个小数点之后: 1/(2^10)
        results = []

        fraction = self.decimal_fraction
        for i in range(precision):
            # devide = fraction / (1/self.to_radix)
            devide = fraction * self.to_radix

            whole = int(devide)
            fraction = devide - whole

            results.append(str(whole))
            if abs(fraction - 1) <= 0.0000001:
                break

        return "0." + "".join(results)

def test():
    s = Solution(0.879)
    print(s.binary_fraction())

    s = Solution(0.879, 5)          # 10进制转5进制
    print(s.binary_fraction())

def main():
    print("start main")
    test()

if __name__ == "__main__":
    main()
